<template>
    <!-- 按钮 -->
    <div class="btn-config">
        <FooterButton
            :button-disable="btnOption"
            class="btn-init"
            @handleSave="handleClick('save')"
            @handleNext="handleClick('doNext')"
            @handleBack="handleClick('doBack')"
            @handleClose="handleClick('doClose')"
            @handleDelete="handleClick('delete')"
        />
        <!-- 下一步  -->
        <next-page
            v-if="nextPageVisible"
            ref="nextPage"
            :status="status"
            :route="prjDetail.route"
            :prj-detail="prjDetail"
            :visible.sync="nextPageVisible"
            :activity-inst-id="workFlowData.activityInstID"
            :process-inst-id="workFlowData.processInstID"
            :activity-def-id="workFlowData.activityDefID"
            :process-def-name="workFlowData.processDefName"
            @doNextEntity="nextPageRes"
        />
        <!--  回退 -->
        <Back-page
            v-if="backPageVisible"
            ref="backPage"
            :visible.sync="backPageVisible"
            :process-inst-id="workFlowData.processInstID"
            :act-inset-id="workFlowData.activityInstID"
            @doReturn="backPageRes"
        />
    </div>
</template>

<script>
import FooterButton from '@/components/footer/details'
import NextPage from '@/views/adjustProject/components/NextPage.vue'  //不用之前的 这个里面不涉及选人
import BackPage from '@/components/nextPerson/BackPage'
import {clonePage, disableScroll, enableScroll, msgInfoEl} from "@/utils";
import {getBackActivities} from "@/api/workFlow";
import {startAdjustPrjFlow,doNextAdjustPrjFLow} from "@/api/adjustProject";
export default {
    name: 'ButtonProcess',
    components: {
        NextPage,
        BackPage,
        FooterButton
    },
    props: {
        workFlowData: {
            type: Object,
            default: () => ({ })
        },
        prjDetail: {
            type: Object,
            default: () => ({ })
        },
        status:{
            type:Boolean,
            default:false
        },
        btnOption:{
            type:Array,
            default:()=>[]
        }
    },
    data() {
        return {
            nextPageVisible: false, // 下一步弹框显示
            backPageVisible: false, // 回退弹框显示
        }
    },
    methods: {
        // 按钮回调方法
        async handleClick(fun) {
            console.log(fun)
            this[fun]()
        },
        save(){
            return msgInfoEl({
                type: 'success',
                message:'保存成功'
            })
        },
        // 下一步按钮主方法
        async doNext() {
            // 赋值打开下一步弹框
            this.nextPageVisible = true
        },
        // 退回按钮主方法
        async doBack() {
            getBackActivities({
                processInstId: this.workFlowData.processInstID,
                actInsetId: this.workFlowData.activityInstID,
                route: '',
                excludeActDefIds: ''
            }).then(res => {
                if(res.data?.length>0){
                    this.backPageVisible = true
                }else{
                    //最后一步回退 直接调接口
                    this.$confirm('请确认是否取消本次跨项目调整流程？确定后您申请的跨项目调整流程将被取消。', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        cancelButtonClass: 'pms-zr-button',
                        confirmButtonClass: 'pms-zr-button-primary',
                        closeOnClickModal: false
                    }).then(() => {
                        const ifNextVal = '2'
                        this.nextBackPageRes({}, ifNextVal)
                    }).catch(() => {
                        console.log('取消删除了')
                    })
                }
            })
        },
        delete(){
            this.$confirm('请确认是否删除？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                cancelButtonClass: 'pms-zr-button',
                confirmButtonClass: 'pms-zr-button-primary',
                closeOnClickModal: false
            }).then(() => {
                //进行撤销操作
                //最后一步回退 直接调接口
                const ifNextVal = '2'
                this.nextBackPageRes({}, ifNextVal)
            }).catch(() => {
                console.log('取消关闭了')
            })
        },
        // 关闭页面主方法
        doClose() {
            clonePage()
        },
        // 下一步确认回调
        nextPageRes(item) {
            const ifNextVal = '0' // 下一步传 0
            this.nextBackPageRes(item, ifNextVal)
            // if(this.status){
            //     //发起下一步
            //     const params={
            //         oldPrjId: this.prjDetail.oldPrjId,
            //         newPrjId: this.prjDetail.newPrjId,
            //         categoryCode: this.prjDetail.categoryCode,
            //         transferNum: this.prjDetail.transferNum,
            //         subPrjId: this.prjDetail.subPrjId,
            //         assetLabelId: this.prjDetail.assetLabelId,
            //         nextAuditUserAccount: item.empId,
            //         opinion: item.opinion,
            //         newPrjName:this.prjDetail.newPrjName,
            //         role: this.prjDetail.role,
            //         bizSeq:this.prjDetail.bizSeq
            //     }
            //     startAdjustPrjFlow(params).then((res)=>{
            //         if (+res.code === 200) {
            //             // 关闭页面
            //             this.doClose()
            //         }
            //     })
            // }else{
            //     //审核流程下一步
            //      this.nextBackPageRes(item, ifNextVal)
            // }
        },
        // 退回确认回调
        backPageRes(item) {
            const ifNextVal = '1' // 回退传1
            this.nextBackPageRes(item, ifNextVal)
        },
        // 下一步 && 回退 &&转派 确认函数
        async nextBackPageRes(item, ifNextVal) {
            console.log(item, ifNextVal, 'ifNextVal')
            let params={}
            // 下一步
            console.log(1111,item)
            if (ifNextVal === '0') {
                params={
                    workItemId: this.workFlowData.workItemID,
                    userIds: item.userId ? item.userId : item.empId,
                    route: this.workFlowData.activityDefID === 'adjust_prj1' ? item.route : '',
                    wfXpath:[],
                    opinion: item.opinion,
                    nextSceneId:item.sceneId,
                    nodeId:item.nodeId,
                    detailId: item?.detailId,
                    activityDefID: this.workFlowData.activityDefID,
                    templateId: item?.templateId,
                }
            } else if (ifNextVal === '1') {
                params={
                    workItemId:this.workFlowData.workItemID,
                    opinion: item.opinion,
                    detailId: item?.detailId,
                    nextActDefId:item.nodeId
                }
            } else if(ifNextVal === '2'){
                params={
                    processInstId:this.workFlowData.processInstID,
                }
            } else{
                console.log('啥也不干')
            }
            const data = {
                flowId: this.workFlowData.bizObject?.bizSeq,
                flowFlag: ifNextVal,
                state: ifNextVal === '2' ? 'C' : item.nodeId,
                wfNextDealParam:params,
            }
            const finalNextFlow = await doNextAdjustPrjFLow(data)
            if (+finalNextFlow.code === 200) {
                // 关闭页面
                this.doClose()
            }
        },
    }
}

</script>
<style lang="scss" scoped>
.btn-config {
    width: 100%;
    padding: 15px;
    // border: 1px solid #EBEEF5;
    display: flex;
    align-items: center;
    justify-content: center;

    .btn-init {
        width: 100%;
    }
}

.el-message--error {
    white-space: pre-line
}

</style>
